package com.jlastu.androiduidemo.views;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewStub;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

import com.example.myapplication.DialogModal;
import com.example.myapplication.MainActivity;
import com.jlastu.androiduidemo.R;
import com.jlastu.androiduidemo.utils.SpUtils;
import com.jlastu.androiduidemo.widget.HeaderView;


//设置界面
public class SettingsActivity extends AppCompatActivity implements View.OnClickListener {

   private HeaderView mHeaderView;
   private Button mBtnLogout;
   private SpUtils mSpUtils;
   private SwitchCompat mAdminSwitch;
   private RelativeLayout mRlAddStudent;
   private Button mBtnChangePassword;
   private Button btn1;
   private Button btn2;
   private Button btn3;
   @Override
   protected void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_settings);
      mHeaderView = findViewById(R.id.header_view);
      mHeaderView.setRightVisibility(View.GONE);
      mHeaderView.setTitle("设置");

      mAdminSwitch = findViewById(R.id.admin_switch);
      mAdminSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
         @Override
         public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            showAdminViews(isChecked);

         }
      });
      mRlAddStudent = findViewById(R.id.add_student_view);
      mRlAddStudent.setOnClickListener(this);

      mBtnLogout = findViewById(R.id.logout_button);
      mBtnLogout.setOnClickListener(this);


      mBtnChangePassword = findViewById(R.id.logout_button1); // 初始化修改密码按钮
      mBtnChangePassword.setOnClickListener(this); // 设置修改密码按钮的点击事件监听器
      mSpUtils = new SpUtils(this);


      //跳转添加课程界面
      btn1=findViewById(R.id.btn3);

      // 监听点击事件
      btn1.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            // 跳转到另一个名为ButtonActivity的界面
            Intent intent=new Intent();
            intent.setClass(SettingsActivity.this, DialogModal.class);
            startActivity(intent);
         }
      });

      //设置界面管理员模式跳转添加学生界面按钮
      btn2=findViewById(R.id.btn2);
      // 监听点击事件
      btn2.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            // 跳转到另一个名为ButtonActivity的界面
            Intent intent=new Intent();
            intent.setClass(SettingsActivity.this,AddStudentActivity.class);
            startActivity(intent);
         }
      });

      //跳转修改密码界面按钮
      btn3=findViewById(R.id.logout_button1);
      btn3.setOnClickListener(new View.OnClickListener() {
         @Override
         public void onClick(View v) {
            Intent intent = new Intent(SettingsActivity.this, AlterKeyActivity.class);
            startActivity(intent);

         }
      });

   }

   //退出登录
   @Override
   public void onClick(View v) {
      int id = v.getId();
      if (id == R.id.logout_button) { //退出登录
         mSpUtils.putInt(SpUtils.LOGIN_KEY, -1);
         Intent intent = new Intent(SettingsActivity.this, LoginActivity.class);
         intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(intent);
      }

   }

   private void showAdminViews(boolean show) {
      View view = findViewById(R.id.admin_views);
      view.setVisibility(show ? View.VISIBLE : View.GONE);
   }
}
